package zuoye;

import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Transferjpanel extends JPanel implements ActionListener {
    private Icon sureimage = Util.getIcon("/images/buttonSure.jpg");
    private Icon offimage = Util.getIcon("/images/buttonOff.jpg");
    private JButton SurejButton;
    private JButton OffjButton;
    private Mainjframe frame;
    private JTextField textField1;
    private JTextField textField2;
    private JLabel jLabel;
    private JLabel jLabel1;
    private boolean result = false;
    private int dindex = 0;
    public Transferjpanel(Mainjframe frame){
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        jLabel = new JLabel("请输入收款人姓名：");
        jLabel.setForeground(Color.BLACK);
        jLabel.setFont(new Font("宋体",Font.BOLD,22));
        jLabel.setBounds(340, 220, 200, 40);
        this.add(jLabel);

        textField1 = new JTextField();
        textField1.setBounds(550, 220, 350, 40);
        this.add(textField1);

        jLabel1 = new JLabel("请输入转账金额：");
        jLabel1.setForeground(Color.BLACK);
        jLabel1.setFont(new Font("宋体",Font.BOLD,22));
        jLabel1.setBounds(340, 280, 200, 40);
        this.add(jLabel1);

        textField2 = new JTextField();
        textField2.setBounds(550, 280, 350, 40);
        this.add(textField2);

        SurejButton = new JButton(sureimage);
        SurejButton.setBounds(360, 500, 140, 50);
        this.add(SurejButton);
        SurejButton.addActionListener(this);
        OffjButton = new JButton(offimage);
        OffjButton.setBounds(700, 500, 140, 50);
        this.add(OffjButton);
        OffjButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == SurejButton) {
            ArrayList<User> user = (ArrayList<User>)FileUtil.readObject("User.dat");
            if (textField1.getText().equals(user.get(frame.getIndex()).getUsername())) {
                JOptionPane.showMessageDialog(this, "不能转账给自己");
                return;
            }
            for (int i = 0; i < user.size(); i++) {
                if (textField1.getText().equals(user.get(i).getUsername())) {
                    result = true;
                    dindex = i;
                    break;
                }
            }
            if (result){
                double a = Double.valueOf(textField2.getText());
                double b = user.get(frame.getIndex()).getMoney();
                if (a <= b){
                    JOptionPane.showMessageDialog(this,"您本次转账" + a + "元");
                    user.get(frame.getIndex()).setMoney(b - a);
                    user.get(dindex).setMoney(frame.getUsers().get(dindex).getMoney() + a);
                    FileUtil.writeObject("User.dat",user);
                    textField1.setText("");
                    frame.switchPanel("menu");
                } else {
                    JOptionPane.showMessageDialog(this,"您的余额不足");
                }
            }else {
                JOptionPane.showMessageDialog(this,"查无此人");
                textField1.setText("");
            }
        } else if (e.getSource() == OffjButton) {
            frame.switchPanel("menu");
        }
    }
}
